<template>
  <div class="app-container">
  
    <el-form ref="queryForm" :model="queryParams" :rules="rules" label-width="80px">
        <el-form-item label="单位名称" prop="companyName">
          <el-input v-model="queryParams.companyName" placeholder="请输入单位名称"  
          clearable
           @keyup.enter.native="handleQuery"/>
        </el-form-item>
      
      </el-form>

 
       <el-row type="flex"  :gutter="10" justify="center">   
          <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd1"
        >下一步</el-button>
         
    
  
      </el-col>
         </el-row>
   <div v-for="(row, index) in titleList"  style="padding-top:10px" >
    <el-card :body-style="{ padding: '0px' }">
      <el-row :gutter="10">
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <div style="padding: 10px;">
          <span   @click="handleUpdate(row)" style="color: #303133;font-size: 18px;font-weight: 700;"> {{ row.companyName }} {{ row.companyType }} {{row.areaName}}</span>
          <div style="padding-top:10px;">
           2024年计划招聘人数:<time class="time" style="padding-left:3px">{{row.currentPostCount}}</time> 人
            编内:<time class="time" style="padding-left:3px">{{row.currentInnerCount}}</time> 人
           <div style="padding:10px;color: #999;">
           <table>
            <tr>
            <td width="20%">学历要求</td>
            <td width="60%">专业</td>
            <td width="20%">招聘/已招</td>
           </tr>
           <tr v-for="position in row.positionList1">
            <td>{{position.educationaRequirement}}</td>
            <td>{{position.professionName}}</td>
            <td>{{position.workCount}}/{{position.hasCount}}</td>
           </tr>
           </table>
            </div>
          </div>
          <div style="padding-top:10px;">
             2025年计划招聘人数:<time class="time" style="padding-left:3px">{{ row.nextPostCount }}</time> 人
              编内:<time class="time" style="padding-left:3px">{{row.nextInnerCount}}</time> 人
           <div style="padding:10px;color: #999;">
           <table>
            <tr>
            <td width="20%">学历要求</td>
            <td width="60%">专业</td>
            <td width="20%">招聘数量</td>
           </tr>
           <tr v-for="position in row.positionList2">
            <td>{{position.educationaRequirement}}</td>
            <td>{{position.professionName}}</td>
            <td>{{position.workCount}}</td>
           </tr>
           </table>
            </div>
          </div>
          <div style="padding-top:10px;">
             2026至2029年计划招聘人数:<time class="time" style="padding-left:3px">{{row.futurePostCount}}</time> 人
              编内:<time class="time" style="padding-left:3px">{{row.futureInnerCount}}</time> 人
                   <div style="padding:10px;color: #999;">
           <table>
            <tr>
             <td width="20%">学历要求</td>
            <td width="60%">专业</td>
            <td width="20%">招聘数量</td>
           </tr>
           <tr v-for="position in row.positionList3">
            <td>{{position.educationaRequirement}}</td>
            <td>{{position.professionName}}</td>
            <td>{{position.workCount}}</td>
           </tr>
           </table>
            </div>
            
          </div>
            <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(row)"
          >修改</el-button>
              <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(row)"
          >删除</el-button>
          
         </div>
        </el-col>
      </el-row>
    </el-card>
    </div>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改招聘信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单位名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="上一年份计草料" prop="prvePostCount">
          <el-input v-model="form.prvePostCount" placeholder="请输入上一年份计草料" />
        </el-form-item>
        <el-form-item label="当前年份" prop="currentPostCount">
          <el-input v-model="form.currentPostCount" placeholder="请输入当前年份" />
        </el-form-item>
        <el-form-item label="未来4年" prop="nextPostCount">
          <el-input v-model="form.nextPostCount" placeholder="请输入未来4年" />
        </el-form-item>
        <el-form-item label="年份" prop="year">
          <el-input v-model="form.year" placeholder="请输入年份" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">

          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" href="../template_V2.xlsx" target="blank">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    
  </div>
</template>

<script>
import { listTitle, getTitle, delTitle, addTitle, updateTitle } from "@/api/system/title";
import { getToken } from "@/utils/auth";

export default {
  name: "aTitle",
  dicts: ['work_compay_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      addNewBtn: false,
      // 总条数
      total: 0,
      // 招聘信息表格数据
      titleList: [],
      yearList:['2024','2025','2026','2027'],
      // 弹出层标题
      title: "",
      lable1:"",
      lable2:"",
      lable3:"",
      lableplan1:"",
      lableplan2:"",
      lableplan3:"",      
      lableinner1:"",
      lableinner2:"",
      lableinner3:"",
      
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        companyType: null,
        year: null,
        area: null,
        status: null,
        prvePostCount: null,
        currentPostCount: null,
        nextPostCount: null,
        year: null,
      },
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/title/importData"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyName: [
          { required: true, message: "单位名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.queryParams.year=new Date().getFullYear();
    this.queryParams.companyName=this.$route.query.companyName;
    this.getList();

  },
  methods: {
    /** 查询招聘信息列表 */
    getList() {
      this.queryParams.year=new Date().getFullYear();
      this.lable1=this.queryParams.year+"年计划招聘人数";
      this.lable2=(parseInt(this.queryParams.year)+1)+"年计划招聘人数";
      this.lable3=(parseInt(this.queryParams.year)+2)+"至"+(parseInt(this.queryParams.year)+5)+"年计划招聘人数";
      
      if(this.queryParams.companyName == null || this.queryParams.companyName==''){
         return ;
      }

      this.loading = true;
      listTitle(this.queryParams).then(response => {
        this.titleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    
    handAddList() {
      this.queryParams.year=new Date().getFullYear();
      this.lable1=this.queryParams.year+"年计划招聘人数";
      this.lable2=(parseInt(this.queryParams.year)+1)+"年计划招聘人数";
      this.lable3=(parseInt(this.queryParams.year)+2)+"至"+(parseInt(this.queryParams.year)+5)+"年计划招聘人数";
      
      if(this.queryParams.companyName == null || this.queryParams.companyName==''){
         return ;
      }
      

      this.loading = true;
      listTitle(this.queryParams).then(response => {
        this.titleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        employId: null,
        companyName: null,
        companyType: null,
        area: null,
        status: null,
        prvePostCount: null,
        currentPostCount: null,
        nextPostCount: null,
        year: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
        this.$refs["queryForm"].validate(valid => {
        if (valid) {
            this.queryParams.pageNum = 1;
             this.getList();
           }
      });

    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.employId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加招聘信息";
    },
    
    handleAdd1() {   
      this.queryParams.year=new Date().getFullYear();
      this.lable1=this.queryParams.year+"年计划招聘人数",
      this.lable2=(parseInt(this.queryParams.year)+1)+"年计划招聘人数",
      this.lable3=(parseInt(this.queryParams.year)+2)+"至"+(parseInt(this.queryParams.year)+5)+"年计划招聘人数",
       
      this.$refs["queryForm"].validate(async(valid) => {
         
         
        if (valid) {
             let  response = await listTitle(this.queryParams);
             this.titleList = response.rows;
             this.total = response.total;
             this.addNewBtn = true;
             if(this.total == 0){
               // window.location.href="../work/workAdd?request=2"
               this.$router.push("/work/addWork?request=2&companyName="+this.queryParams.companyName);
             }
           }
      });
    },
   
      handleAdd2(add) {    
      this.$refs["queryForm"].validate(async(valid) => {
         
         
        if (valid) {
             // window.location.href="../work/workAdd?request=2"
               this.$router.push("/work/addWork?request=2&companyName="+this.queryParams.companyName);
           }
      });
    },
    
    handleUpdate(row) {
         const employId = row.employId || this.ids;
         this.$router.push("/work/addWork?&request=2&employId="+employId);
    },

    /** 修改按钮操作 */
    handleUpdate1(row) {
      this.reset();
      const employId = row.employId || this.ids
      getTitle(employId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改招聘信息";
      });
    },
    
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.employId != null) {
            updateTitle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTitle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const employIds = row.employId || this.ids;
      this.$modal.confirm('是否确认删除招聘信息编号为"' + employIds + '"的数据项？').then(function() {
        return delTitle(employIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
        /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/title/importTemplate', {
      }, `work_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/title/export', {
        ...this.queryParams
      }, `title_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
